﻿using NetCore8Demo.Models.Param;
using System.ComponentModel;

namespace NetCore8Demo.Views.Param
{
    public partial class BasicParamForm : Form
    {
        //构造方法
        public BasicParamForm(List<ParamInfo> paramList)
        {
            InitializeComponent();
            _paramList = paramList;
            Init();

        }

        private TableLayoutPanel _tableLayoutPanel = new ();
        private List<ParamInfo> _paramList;

        private int _rowHeight = 45;
        private int _col1Weight = 160;
        private int _col2Weight = 160;

        public bool SureFlag;

        private void BasicParamForm_Load(object sender, EventArgs e)
        {


        }

        #region 初始化绘制页面

        public void Init()
        {
            int rowCount = _paramList.Count;

            this.Height = _rowHeight * rowCount + 150;
            this.Width = _col1Weight + _col2Weight + 30;

            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterParent;


            _tableLayoutPanel.Location = new Point(5, 5);
            _tableLayoutPanel.RowCount = rowCount;
            _tableLayoutPanel.ColumnCount = 2; // 设置列数
            _tableLayoutPanel.Size = new Size(_col1Weight + _col2Weight, _rowHeight * rowCount);
            //第一列宽度
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _col1Weight));
            //第二列宽度
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _col2Weight));
            //所有行高
            for (int i = 0; i < _tableLayoutPanel.RowCount; i++)
            {
                _tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, _rowHeight));
            }

            //显示网格线
            // _tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            //添加参数控件
            for (int i = 0; i < _paramList.Count; i++)
            {
                // 第一个参数是列索引，第二个参数是行索引
                _tableLayoutPanel.Controls.Add(GetLabel(_paramList[i]), 0, i);
                GetLabel(_paramList[i]).Anchor = AnchorStyles.Right;
                switch (_paramList[i].ParamType)
                {
                    case ParamType.ComboBox:
                        _tableLayoutPanel.Controls.Add(GetComboBox(_paramList[i]), 1, i);
                        break;
                    case ParamType.TextBox:
                        _tableLayoutPanel.Controls.Add(GetTextBox(_paramList[i]), 1, i);
                        break;
                    case ParamType.YesOrNo:
                        _tableLayoutPanel.Controls.Add(GetYesOrNoPanel(_paramList[i]), 1, i);
                        break;
                }
            }

            //添加确认按钮
            Button sureBtn = new Button();
            sureBtn.Text = "确认";
            sureBtn.Location = new Point((this.Width - 100) / 2, _rowHeight * rowCount + 50);
            sureBtn.Size = new Size(100, 45); // 设置固定大小
            sureBtn.Click += SureBtn_Click;

            this.Controls.Add(_tableLayoutPanel);
            this.Controls.Add(sureBtn);

        }

        #endregion

        #region 页面按钮
        private void SureBtn_Click(object? sender, EventArgs e)
        {

            for (int row = 0; row < _paramList.Count; row++)
            {
                Control? control = _tableLayoutPanel.GetControlFromPosition(1, row); // 列索引从0开始，所以第2列的索引是1
                if (control != null)
                {
                    if (control is TextBox textBox)
                    {
                        _paramList[row].NewValue = textBox.Text.Trim();
                    }

                    if (control is ComboBox comboBox)
                    {
                        _paramList[row].NewValue = comboBox.Text.Trim();
                    }

                    if (control is Panel panel)
                    {
                        _paramList[row].NewValue = "false";
                        foreach (Control radioControl in panel.Controls)
                        {
                            // 你可以在这里对找到的控件进行操作
                            if (radioControl is RadioButton radioButton)
                            {
                                if (radioButton.Name.Contains("Yes")&& radioButton.Checked)
                                {
                                    _paramList[row].NewValue = "true";
                                    break;
                                }
                               
                            }
                           
                        }
                    }

                }
            }

            SureFlag = true;
            this.Close();
        }

        #endregion

        #region  生成不同控件
        public Label GetLabel(ParamInfo paramInfo)
        {
            
            Label label = new Label();
            label.Text = paramInfo.LabelName + ":";
            label.AutoSize = false;
            label.Size = new Size(_col1Weight - 5, 36); // 设置固定大小
            label.Anchor = AnchorStyles.Right;
            label.Font = new Font(label.Font.FontFamily, 12, label.Font.Style);
            label.TextAlign = ContentAlignment.MiddleRight;
            return label;
        }

        public TextBox GetTextBox(ParamInfo paramInfo)
        {
            TextBox textBox = new TextBox();
            textBox.Text = paramInfo.OldValue;
            textBox.Size = new Size(_col2Weight - 10, 36); // 设置固定大小
            textBox.Anchor = AnchorStyles.Left;
            textBox.Font = new Font(textBox.Font.FontFamily, 12, textBox.Font.Style);
            textBox.TextAlign = HorizontalAlignment.Left;
            return textBox;
        }

        public ComboBox GetComboBox(ParamInfo paramInfo)
        {
            ComboBox comboBox = new ComboBox();
            comboBox.Text = paramInfo.OldValue;
            comboBox.Size = new Size(_col2Weight - 5, 36); // 设置固定大小
            comboBox.Anchor = AnchorStyles.Left;
            comboBox.Font = new Font(comboBox.Font.FontFamily, 12, comboBox.Font.Style);
            foreach (string item in paramInfo.ValueList)
            {
                comboBox.Items.Add(item);
            }

            return comboBox;
        }


        public Panel GetYesOrNoPanel(ParamInfo paramInfo)
        {
            Panel panel = new Panel();
            RadioButton radioButtonYes = new RadioButton();
            RadioButton radioButtonNo = new RadioButton();

            radioButtonYes.Font = new Font(radioButtonYes.Font.FontFamily, 10, radioButtonYes.Font.Style);
            radioButtonNo.Font = new Font(radioButtonYes.Font.FontFamily, 10, radioButtonYes.Font.Style);

            // 初始化 Panel
            panel.Dock = DockStyle.Fill;
          

            // 初始化 "是" 单选框
            radioButtonYes.Text = "是";
            radioButtonYes.Name = "Yes";
            radioButtonYes.Location = new System.Drawing.Point(5, 7);
            radioButtonYes.Size = new Size(40, 25);
            // 初始化 "否" 单选框
            radioButtonNo.Text = "否";
            radioButtonNo.Name = "No";
            radioButtonNo.Location = new System.Drawing.Point(_col2Weight/2+5, 7);
            radioButtonYes.Size = new Size(40, 25);
            // 将单选框添加到 Panel
            panel.Controls.Add(radioButtonYes);
            panel.Controls.Add(radioButtonNo);
            bool.TryParse(paramInfo.OldValue, out bool flag);

            radioButtonYes.Checked = flag;
            radioButtonNo.Checked = !flag;

            return panel;
        }

        #endregion


    }
}
